expect contents2
read /dir/test2
-# Creating dir over the top should fail.
-expect mkdir failed: File exists
+# Creating dir over the top should succeed.
mkdir /dir
-expect mkdir failed: File exists
mkdir /dir/test2
# Mkdir implicitly creates directories.
-# Remove non-existant fails.
-expect rm failed: No such file or directory
+# Remove non-existant is OK, as long as parent exists
rm /test
expect rm failed: No such file or directory
rm /dir/test
return dir;
}
+static bool node_exists(struct connection *conn, const char *node)
+{
+ struct stat st;
+
+ return lstat(node_dir(conn->transaction, node), &st) == 0;
+}
+
/* path, flags, data... */
static void do_write(struct connection *conn, struct buffered_data *in)
{
static void do_mkdir(struct connection *conn, const char *node)
{
char *dir;
- struct stat st;
node = canonicalize(conn, node);
if (!check_node_perms(conn, node, XS_PERM_WRITE|XS_PERM_ENOENT_OK)) {
if (transaction_block(conn, node))
return;
- /* Must not already exist. */
- if (lstat(node_dir(conn->transaction, node), &st) == 0) {
- send_error(conn, EEXIST);
+ /* If it already exists, fine. */
+ if (node_exists(conn, node)) {
+ send_ack(conn, XS_MKDIR);
return;
}
node = canonicalize(conn, node);
if (!check_node_perms(conn, node, XS_PERM_WRITE)) {
+ /* Didn't exist already? Fine, if parent exists. */
+ if (errno == ENOENT) {
+ if (node_exists(conn, get_parent(node))) {
+ send_ack(conn, XS_RM);
+ return;
+ }
+ /* Restore errno, just in case. */
+ errno = ENOENT;
+ }
send_error(conn, errno);
return;
}
}
/* Create a new directory.
- * Returns false on failure.
+ * Returns false on failure, or success if it already exists.
*/
bool xs_mkdir(struct xs_handle *h, const char *path)
{
}
/* Destroy a file or directory (directories must be empty).
- * Returns false on failure.
+ * Returns false on failure, or success if it doesn't exist.
*/
bool xs_rm(struct xs_handle *h, const char *path)
{
unsigned int len, int createflags);
/* Create a new directory.
- * Returns false on failure.
+ * Returns false on failure, or success if it already exists.
*/
bool xs_mkdir(struct xs_handle *h, const char *path);
/* Destroy a file or directory (and children).
- * Returns false on failure.
+ * Returns false on failure, or success if it doesn't exist.
*/
bool xs_rm(struct xs_handle *h, const char *path);
make_dirs(parent_filename(dirname));
if (mkdir(dirname, 0700) != 0)
- return false;
+ return (errno == EEXIST);
init_perms(dirname);
return true;
return false;
}
- if (lstat(filename, &st) != 0)
- return false;
+ if (lstat(filename, &st) != 0) {
+ if (lstat(parent_filename(filename), &st) != 0)
+ return false;
+ return true;
+ }
if (!write_ok(info, path))
return false;